package com.qxsx.midi.fragment;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.exam.ExamMyListActivity;
import com.qxsx.midi.activity.me.ApplyShowActivity;
import com.qxsx.midi.activity.me.LoginActivity;
import com.qxsx.midi.activity.me.MeKejiantActivity;
import com.qxsx.midi.activity.me.MeMsgListActivity;
import com.qxsx.midi.activity.me.MyOrderActivity;
import com.qxsx.midi.activity.me.SystemSetActivity;
import com.qxsx.midi.activity.personal.DownLodeMusciActivity;
import com.qxsx.midi.activity.personal.PersonalDetailsActivity;
import com.qxsx.midi.activity.personal.PersonalDetailsNewActivity;
import com.qxsx.midi.activity.study.AboutMiDiActivity;
import com.qxsx.midi.activity.study.AboutMiDiMaxActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.CircleImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Joney on 2015/8/31.
 */
public class MeFragment extends Fragment implements View.OnClickListener {
    private CustomProgress customProgress;

    private TextView tv_register, tv_login;
    private CircleImageView ci_head;//头像
    //申请展示
    private RelativeLayout rl_apply, rl_downlode;


    private RelativeLayout meMsg;//我的消息
    private RelativeLayout myOrder;//我的订单
    private RelativeLayout myExam;//我的考试
    private RelativeLayout aboutmidi;//关于迷笛


    //系统设置
    private RelativeLayout rl_systemset;


    private String _id;
    private RelativeLayout rl_no, rl_yes, rl_head;//用户没登录，用户登录了

    private String midi_id, nickname, ismidisir, role_id, level_posion;

    private LinearLayout ll_midimen, ll_lable, ll_text;


    private TextView tv_login_name;//用户名
    private TextView tv_nick_name;//昵称
    private TextView tv_level;//等级
    private TextView tv_name;
    private TextView tv_id;
    private TextView tv_position;

    ImageView iv_icon;


    String login_name, major_id;//用户名

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_me, container, false);


        findByID(v);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        login_name = (String) SPUtils.get(AppContext.getContext(), "login_name", "");

        if (login_name == "") {
            ci_head.setImageResource(R.mipmap.heardicon);
            rl_no.setVisibility(View.VISIBLE);
            rl_yes.setVisibility(View.GONE);

        } else {
            rl_no.setVisibility(View.GONE);
            rl_yes.setVisibility(View.VISIBLE);
            getData();

        }

    }

    private void getData() {
        customProgress = CustomProgress.show(getActivity(), "请稍后....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        _id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        try {
            object.put("user_id", _id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("109", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if ("200".equals(code.toString())) {
                    try {
                        JSONObject object = array.getJSONObject(0);
                        MyNet.setNetImage(getActivity(), ci_head, object.getString("head_img"));
                        midi_id = object.getString("midi_id");
                        nickname = object.getString("nickname");
                        ismidisir = object.getString("ismidisir");
                        role_id = object.getString("role_id");
                        level_posion = object.getString("level");
                        if (nickname.equals("")) {
                            tv_nick_name.setVisibility(View.GONE);

                        } else {
                            tv_nick_name.setVisibility(View.VISIBLE);
                            tv_nick_name.setText(nickname);
                        }
                        tv_login_name.setText(midi_id);
                        if (ismidisir.equals("")) {
                            ll_midimen.setVisibility(View.GONE);
                            if (role_id.equals("100")) {
                                ll_lable.setVisibility(View.VISIBLE);
                                ll_text.setVisibility(View.GONE);
                                ll_lable.removeAllViews();

                                JSONArray array1 = object.getJSONArray("majorList");

                                for (int i = 0; i < array1.length(); i++) {
                                    JSONObject object1 = array1.getJSONObject(i);
                                    major_id = object1.getString("major_id");

                                    View v = LayoutInflater.from(getActivity()).inflate(R.layout.item_lable, null);
                                    iv_icon = (ImageView) v.findViewById(R.id.iv_icon);
                                    if (major_id.toString().equals("2172405d-17bb-5f40-da92-cf9cc285a980")) {
                                        iv_icon.setBackgroundResource(R.mipmap.jita_gw);
                                    } else if (major_id.toString().equals("cd12a552-95ee-9d9b-1efb-8c302c6b52c2")) {
                                        iv_icon.setBackgroundResource(R.mipmap.jazz_gw);
                                    } else if (major_id.toString().equals("5bb9bcb5-88a0-9c85-0a15-39b51c1142be")) {
                                        iv_icon.setBackgroundResource(R.mipmap.bass_gw);
                                    } else if (major_id.toString().equals("87b65e6a-72fc-4617-d61b-351a7d080ed7")) {
                                        iv_icon.setBackgroundResource(R.mipmap.base_gw);
                                    }
                                    tv_name = (TextView) v.findViewById(R.id.tv_name);
                                    tv_name.setText(object1.getString("grade_num"));
                                    ll_lable.addView(v);

                                }
                            } else if (role_id.equals("101")) {
                                ll_lable.setVisibility(View.GONE);
                                ll_text.setVisibility(View.VISIBLE);
                                if (level_posion.equals("1")) {
                                    tv_id.setText("迷笛考官" + "(" + "助理" + ")");
                                } else if (level_posion.equals("2")) {
                                    tv_id.setText("迷笛考官" + "(" + "初级" + ")");

                                } else if (level_posion.equals("3")) {
                                    tv_id.setText("迷笛考官" + "(" + "中级" + ")");

                                } else if (level_posion.equals("4")) {
                                    tv_id.setText("迷笛考官" + "(" + "高级" + ")");
                                }
                            } else if (role_id.equals("102")) {
                                ll_lable.setVisibility(View.GONE);
                                ll_text.setVisibility(View.VISIBLE);
                                tv_id.setText("工作人员");
                            }

                        } else {
                            ll_lable.setVisibility(View.GONE);
                            ll_midimen.setVisibility(View.VISIBLE);
                            ll_text.setVisibility(View.GONE);
                            if (ismidisir.equals("6")) {
                                tv_level.setText("初级");
                            } else if (ismidisir.equals("7")) {
                                tv_level.setText("中级");
                            } else if (ismidisir.equals("8")) {
                                tv_level.setText("高级");
                            } else if (ismidisir.equals("9")) {
                                tv_level.setText("特级");
                            }

                            if (role_id.equals("101")) {
                                if (level_posion.equals("1")) {
                                    tv_position.setText(" & " + "迷笛考官" + "(" + "助理" + ")");
                                } else if (level_posion.equals("2")) {
                                    tv_position.setText(" & " + "迷笛考官" + "(" + "初级" + ")");

                                } else if (level_posion.equals("3")) {
                                    tv_position.setText(" & " + "迷笛考官" + "(" + "中级" + ")");

                                } else if (level_posion.equals("4")) {
                                    tv_position.setText(" & " + "迷笛考官" + "(" + "高级" + ")");
                                }
                            }else if (role_id.equals("102")){
                                tv_position.setText(" & " + "工作人员");

                            }
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                } else {
                    Log.e("code", code.toString());
                    Log.e("message", message.toString());
                }

                customProgress.dismiss();
            }
        });
    }

    private void findByID(View v) {
        tv_register = (TextView) v.findViewById(R.id.tv_register);
        tv_login = (TextView) v.findViewById(R.id.tv_login);
        rl_apply = (RelativeLayout) v.findViewById(R.id.rl_apply);
        rl_apply.setOnClickListener(this);

        myExam = (RelativeLayout) v.findViewById(R.id.rl_myexam);
        myExam.setOnClickListener(this);

        ci_head = (CircleImageView) v.findViewById(R.id.ci_head);
        ci_head.setOnClickListener(this);

        meMsg = (RelativeLayout) v.findViewById(R.id.btn_me_msg);
        meMsg.setOnClickListener(this);

        aboutmidi = (RelativeLayout) v.findViewById(R.id.rl_about);
        aboutmidi.setOnClickListener(this);


        rl_systemset = (RelativeLayout) v.findViewById(R.id.rl_systemset);
        rl_systemset.setOnClickListener(this);

        myOrder = (RelativeLayout) v.findViewById(R.id.btn_my_order);
        myOrder.setOnClickListener(this);

        rl_head = (RelativeLayout) v.findViewById(R.id.rl_head);
        rl_head.setOnClickListener(this);

        rl_no = (RelativeLayout) v.findViewById(R.id.rl_no);
        rl_yes = (RelativeLayout) v.findViewById(R.id.rl_yes);

        tv_login_name = (TextView) v.findViewById(R.id.tv_login_name);
        tv_nick_name = (TextView) v.findViewById(R.id.tv_nick_name);

        ll_midimen = (LinearLayout) v.findViewById(R.id.ll_midimen);

        tv_level = (TextView) v.findViewById(R.id.tv_level);

        rl_downlode = (RelativeLayout) v.findViewById(R.id.rl_downlode);
        rl_downlode.setOnClickListener(this);

        ll_lable = (LinearLayout) v.findViewById(R.id.ll_lable);
        ll_text = (LinearLayout) v.findViewById(R.id.ll_text);

        tv_id = (TextView) v.findViewById(R.id.tv_id);

        tv_position = (TextView) v.findViewById(R.id.tv_position);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.rl_head:
                if (login_name != "") {
                    startActivity(new Intent(getActivity(), PersonalDetailsNewActivity.class));
                    //startActivity(new Intent(getActivity(), DownLodeMusciActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;


            case R.id.rl_downlode:
                //  startActivity(new Intent(getActivity(), PersonalDetailsActivity.class));
                startActivity(new Intent(getActivity(), DownLodeMusciActivity.class));

                break;
            case R.id.rl_myexam://我的考试
                if (login_name != "") {
                    startActivity(new Intent(getActivity(), ExamMyListActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.rl_apply://申请展示

                if (login_name != "") {
                    startActivity(new Intent(getActivity(), ApplyShowActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                //  Toast.makeText(AppContext.getContext(),"该功能还在开发中.....",Toast.LENGTH_SHORT).show();
                break;
            case R.id.rl_about://关于迷笛
                startActivity(new Intent(getActivity(), AboutMiDiMaxActivity.class));
                break;
            case R.id.btn_me_msg://我的消息
                if (login_name != "") {
                    startActivity(new Intent(getActivity(), MeMsgListActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }

                break;
            case R.id.btn_my_order://我的订单
                if (login_name != "") {
                    startActivity(new Intent(getActivity(), MyOrderActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }


                break;

            case R.id.rl_systemset:  //系统设置
                startActivity(new Intent(getActivity(), SystemSetActivity.class));
                break;
            default:
                break;
        }
    }
}
